/*
 * LineAppearancePanel.java
 *
 * Created on June 19, 2005, 10:18 PM
 */
package ika.gui;

import ika.geo.VectorSymbol;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 *
 * @author  jenny
 */
public class LineAppearancePanel extends javax.swing.JPanel {

    private boolean updatingGUI = false;
    private VectorSymbol vectorSymbol;
    private static final float[] LINE_WIDTHS = {0.25f, 0.5f, 1.f, 1.5f, 2.f, 3.f, 5.f, 10.f};
    EventListenerList actionListeners = new EventListenerList();

    /** Creates new form LineAppearancePanel */
    public LineAppearancePanel() {
        initComponents();
        vectorSymbol = new VectorSymbol();
        vectorSymbol.setFilled(false);
        vectorSymbol.setScaleInvariant(true);
        vectorSymbol.setStroked(true);
    }

    ComboBoxModel loadLineIcons() {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (int i = 1; i <= 5; ++i) {
            String path = "/ika/icons/line" + i + ".gif";
            ImageIcon icon = new ImageIcon(getClass().getResource(path));
            model.addElement(icon);
        }
        return model;
    }

    public VectorSymbol getVectorSymbol() {
        this.readVectorSymbolFromGUI();
        return vectorSymbol.copy();
    }

    public void setVectorSymbol(VectorSymbol vectorSymbol) {
        this.vectorSymbol = vectorSymbol.copy();
        this.writeVectorSymbolToGUI();
    }

    private void writeVectorSymbolToGUI() {
        try {
            this.updatingGUI = true;
            // color of lines
            this.lineColorButton.setColor(vectorSymbol.getStrokeColor());

            // line type
            final int lineTypeID = (int) Math.round(vectorSymbol.getDashLength() / 2);
            this.lineTypeComboBox.setSelectedIndex(lineTypeID);

            // line width
            final float lineWidth = vectorSymbol.getStrokeWidth();
            for (int i = 0; i < LINE_WIDTHS.length; i++) {
                if (LINE_WIDTHS[i] == lineWidth) {
                    this.lineWidthComboBox.setSelectedIndex(i);
                    return;
                }
            }
            this.lineWidthComboBox.setSelectedIndex(2);   // default is 1.
        } finally {
            this.updatingGUI = false;
        }
    }

    private void readVectorSymbolFromGUI() {
        if (this.updatingGUI) {
            return;
        }
        // dash length
        int lineType = this.lineTypeComboBox.getSelectedIndex();
        vectorSymbol.setDashLength(lineType * 2);

        // line width of the distortion grid lines
        int lineWidthID = this.lineWidthComboBox.getSelectedIndex();
        final float lineWidth = LINE_WIDTHS[lineWidthID];
        vectorSymbol.setStrokeWidth(lineWidth);

        //line color
        Color color = this.lineColorButton.getColor();
        vectorSymbol.setStrokeColor(color);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        lineColorButton = new ika.gui.ColorButton();
        lineWidthComboBox = new javax.swing.JComboBox();
        lineTypeComboBox = new javax.swing.JComboBox();

        setLayout(new java.awt.GridBagLayout());

        lineColorButton.setToolTipText("Select the line color.");
        lineColorButton.setIconHeight(14);
        lineColorButton.setIconTextGap(0);
        lineColorButton.setIconWidth(14);
        lineColorButton.setMargin(null);
        lineColorButton.setMaximumSize(null);
        lineColorButton.setMinimumSize(new java.awt.Dimension(32, 32));
        lineColorButton.setPreferredSize(new java.awt.Dimension(32, 32));
        lineColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LineAppearancePanel.this.actionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        add(lineColorButton, gridBagConstraints);

        lineWidthComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0.25 pt", "0.5 pt", "1 pt", "1.5 pt", "2 pt", "3 pt", "5 pt", "10 pt" }));
        lineWidthComboBox.setSelectedIndex(2);
        lineWidthComboBox.setToolTipText("Select the line width.");
        lineWidthComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LineAppearancePanel.this.actionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 0, 7);
        add(lineWidthComboBox, gridBagConstraints);

        lineTypeComboBox.setModel(this.loadLineIcons());
        lineTypeComboBox.setToolTipText("Select solid or dashed lines.");
        lineTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LineAppearancePanel.this.actionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        add(lineTypeComboBox, gridBagConstraints);

    }
    // </editor-fold>//GEN-END:initComponents

    private void actionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionPerformed
        if (!this.updatingGUI) {
            this.fireAction(evt);
        }
    }//GEN-LAST:event_actionPerformed

    public void addActionListener(ActionListener listener) {
        actionListeners.add(ActionListener.class, listener);
    }

    public void removeActionListener(ActionListener listener) {
        actionListeners.remove(ActionListener.class, listener);
    }

    protected void fireAction(ActionEvent actionEvent) {
        // loop through each listener and pass on the event if needed
        Object[] listeners = actionListeners.getListenerList();
        int numListeners = listeners.length;
        for (int i = 0; i < numListeners; i++) {
            if (listeners[i] == ActionListener.class) {
                // pass the event to the listeners event dispatch method
                ((ActionListener) listeners[i + 1]).actionPerformed(actionEvent);
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private ika.gui.ColorButton lineColorButton;
    private javax.swing.JComboBox lineTypeComboBox;
    private javax.swing.JComboBox lineWidthComboBox;
    // End of variables declaration//GEN-END:variables
}
